package Persistencia;

import Complementos.Conexion_BD;
import Modelo.Odontologo;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

public class OdontologoDAO {

    Conexion_BD co = new Conexion_BD();
    ResultSet rs = null;
    CallableStatement cs;

    public void agregarOdontologo(Odontologo nuevoOdontologo) throws SQLException {
        try {
            co.Conectar();
            co.getCn().setAutoCommit(false);
            cs = co.getCn().prepareCall("{CALL usp_registrar_odontologo(?,?,?,?,?)}");

            cs.setInt(1, nuevoOdontologo.getCMP());
            cs.setInt(2, nuevoOdontologo.getIdpersona());
            cs.setString(3, nuevoOdontologo.getEspecialidad());
            cs.setString(4, nuevoOdontologo.getFacultad());
            cs.setString(5, nuevoOdontologo.getFechaingreso());

            cs.executeUpdate();
            co.getCn().commit();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PersonaDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PersonaDAO.class.getName()).log(Level.SEVERE, null, ex);
            co.getCn().rollback();
        } finally {
            try {
                co.getCn().setAutoCommit(true);
                co.Desconectar();
            } catch (Exception ex) {
                Logger.getLogger(PersonaDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    public Vector<Vector> listarTodos() {

        Vector<Vector> v = new Vector<Vector>();
        try {

            co.Conectar();
            

            rs = co.Consultar("SELECT nombres,apellidoPaterno,o.CMP,o.id FROM persona p, odontologo o WHERE p.idPersona = o.idPERSONA order by o.fechaingreso");

            if (rs.first()) {
                while (!rs.isAfterLast()) {
                    Vector registro = new Vector();
                    registro.add(rs.getString(1));
                    registro.add(rs.getString(2));
                    registro.add(rs.getString(3));
                    registro.add(rs.getString(4));
                    v.add(registro);
                    rs.next();
                }
            }

            try {
                co.Desconectar();
            } catch (Exception ex) {
                Logger.getLogger(PacienteDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            return v;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PacienteDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PacienteDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return v;


    }

    public int dameCMPDeOdontologo(int idodontologo) {
        int cmp = 0;

        try {

            co.Conectar();

            rs = co.Consultar("SELECT cmp from odontologo where id =" + idodontologo);

            if (rs.first()) {
                while (!rs.isAfterLast()) {
                    cmp = Integer.parseInt(rs.getString(1));
                    rs.next();
                }
            }

            try {
                co.Desconectar();
            } catch (Exception ex) {
                Logger.getLogger(PacienteDAO.class.getName()).log(Level.SEVERE, null, ex);
            }
            return cmp;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(PacienteDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(PacienteDAO.class.getName()).log(Level.SEVERE, null, ex);
        }

        return cmp;


    }
}
